<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <table>
      <thead>
        <tr>
          <th>1</th>
          <th>2</th>
          <th>3</th>
          <th>4</th>
          <th>5</th>
          <th>6</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td rowspan="2">2</td>
          <td>3</td>
          <td>4</td>
          <td rowspan="2">5</td>
          <td>6</td>
          <td>7</td>
        </tr>
        <tr>
          <td rowspan="2">2</td>
          <td>3</td>
          <td>45</td>
          <td>67</td>
        </tr>
        <tr>
          <td>2</td>
          <td>3</td>
          <td>45</td>
          <td>67</td>
        </tr>
        <tr>
          <td>321</td>
          <td>321</td>
          <td>321</td>
          <td>3214</td>
          <td>543</td>
          <td>657</td>
        </tr>
      </tbody>
    </table>
    <script>
      let arr = [
        {
          title: "src",
          folder: true,
          child: [
            {
              title: "src/a",
              folder: true,
              child: [
                {
                  title: "src/w/green",
                },
                {
                  title: "src/w/red",
                },
              ],
            },
            {
              title: "src/b",
              folder: true,
              child: [
                {
                  title: "src/b/yellow",
                },
                {
                  title: "src/b/blue",
                },
              ],
            },
          ],
        },
      ];

      function fn(arr, value) {
        console.log(2);
        let saveResult = [];
        for (let i = 0; i < arr.length; i++) {
          let flag = arr[i].title.includes(value);
          if (arr[i].folder) {
            let { childSaveResult } = fn(arr[i].child, value);
            // 有子集 继续遍历子集
            // 查询到 存到集合 并且全部返回 覆盖原来子集 继续遍历子集
            if (flag) {
              if (childSaveResult.length > 0) {
                arr[i].child = childSaveResult;
                saveResult.push(
                  Object.assign(arr[i], { search: flag, collspace: flag })
                );
              } else {
                arr[i].child = [];
                saveResult.push(
                  Object.assign(arr[i], { search: flag, collspace: flag })
                );
              }
            } else {
              // 查询不到 遍历子集
              if (childSaveResult.length > 0) {
                arr[i].collspace = true;
                arr[i].child = childSaveResult;
                saveResult.push(Object.assign(arr[i], { search: flag }));
              }
            }
          } else {
            // 无子集 停止函数
            if (flag) {
              obj = Object.assign(arr[i], { collspace: flag, search: flag });
              saveResult.push(obj);
              console.log(saveResult);
            }
          }
        }
        return {
          childSaveResult: saveResult,
        };
      }

      console.log(fn(arr, ""));
    </script>
  </body>
</html>
